﻿
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using Vanara.PInvoke;

namespace DataAcquisition
{
    public partial class AddTemplateForm : BaseForm
    {
        #region 属性

        private Template obj = new Template();
        #endregion

        #region 自定义方法

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = NumberInput.Text;
            List<Template> list = SqlHelper.Inquireable<Template>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】模板已存在", where["number"]);
                Error(msg);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        #endregion

        #region 系统方法
        public AddTemplateForm()
        {
            InitializeComponent();
            Header.Text = "添加试压模板";
        }

        private void AddTemplateForm_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (NumberInput.Text.IsNullOrEmpty())
            {
                Error("请输入编号");
                return;
            }
            bool res = CheckRepeat();
            if (res)
            {
                return;
            }
            obj.Number = NumberInput.Text.ObjToInt();

            if (PipePositionSelect.Text.IsNullOrEmpty())
            {
                Error("请选择管道位置");
                return;
            }
            obj.PipePosition = PipePositionSelect.Text;

            if (DesignPressureInput.Text.IsNullOrEmpty())
            {
                Error("请输入设计压力");
                return;
            }
            obj.DesignPressure = Math.Round(double.Parse(DesignPressureInput.Text), 3);

            int stageCount = 0;

            if (FirstStagePressureInput.Text.Length > 0)
            {
                obj.FirstStagePressure = Math.Round(double.Parse(FirstStagePressureInput.Text), 3);

                if (FirstStageTimeInput.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段一持续时间");
                    return;
                }
                obj.FirstStageTime = FirstStageTimeInput.Text.ObjToInt();

                if (FirstStageTimeUnitSelect.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段一时间单位");
                    return;
                }
                obj.FirstStageTimeUnit = FirstStageTimeUnitSelect.Text;

                if (FirstStageEstimateWaySelect.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段一判断方式");
                    return;
                }
                obj.FirstStageEstimateWay = FormatEstimate(FirstStageEstimateWaySelect.Text);

                if (FirstStageEstimateValueInput.Text.IsNullOrEmpty())
                {
                    Error("请输入阶段一判断数值");
                    return;
                }
                obj.FirstStageEstimateValue = Math.Round(double.Parse(FirstStageEstimateValueInput.Text), 3);
                stageCount++;
            }

            if (SecondStagePressureInput.Text.Length > 0)
            {
                obj.SecondStagePressure = Math.Round(double.Parse(SecondStagePressureInput.Text), 3);

                if (SecondStageTimeInput.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段二持续时间");
                    return;
                }
                obj.SecondStageTime = SecondStageTimeInput.Text.ObjToInt();

                if (SecondStageTimeUnitSelect.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段二时间单位");
                    return;
                }
                obj.SecondStageTimeUnit = SecondStageTimeUnitSelect.Text;

                if (SecondStageEstimateWaySelect.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段二判断方式");
                    return;
                }
                obj.SecondStageEstimateWay = FormatEstimate(SecondStageEstimateWaySelect.Text);

                if (SecondStageEstimateValueInput.Text.IsNullOrEmpty())
                {
                    Error("请输入阶段二判断数值");
                    return;
                }
                obj.SecondStageEstimateValue = Math.Round(double.Parse(SecondStageEstimateValueInput.Text), 3);
                stageCount++;
            }

            if (ThirdStagePressureInput.Text.Length > 0)
            {
                obj.ThirdStagePressure = Math.Round(double.Parse(ThirdStagePressureInput.Text), 3);

                if (ThirdStageTimeInput.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段三持续时间");
                    return;
                }
                obj.ThirdStageTime = ThirdStageTimeInput.Text.ObjToInt();

                if (ThirdStageTimeUnitSelect.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段三时间单位");
                    return;
                }
                obj.ThirdStageTimeUnit = ThirdStageTimeUnitSelect.Text;

                if (ThirdStageEstimateWaySelect.Text.IsNullOrEmpty())
                {
                    Error("请选择阶段三判断方式");
                    return;
                }
                obj.ThirdStageEstimateWay = FormatEstimate(ThirdStageEstimateWaySelect.Text);

                if (ThirdStageEstimateValueInput.Text.IsNullOrEmpty())
                {
                    Error("请输入阶段三判断数值");
                    return;
                }
                obj.ThirdStageEstimateValue = Math.Round(double.Parse(ThirdStageEstimateValueInput.Text), 3);
                stageCount++;
            }

            if (IntensityStagePressureInput.Text.Length > 0)
            {
                obj.IntensityStagePressure = Math.Round(double.Parse(IntensityStagePressureInput.Text), 3);

                if (IntensityStageTimeInput.Text.IsNullOrEmpty())
                {
                    Error("请选择强度段持续时间");
                    return;
                }
                obj.IntensityStageTime = IntensityStageTimeInput.Text.ObjToInt();

                if (IntensityStageTimeUnitSelect.Text.IsNullOrEmpty())
                {
                    Error("请选择强度段时间单位");
                    return;
                }
                obj.IntensityStageTimeUnit = IntensityStageTimeUnitSelect.Text;

                if (IntensityStageEstimateWaySelect.Text.IsNullOrEmpty())
                {
                    Error("请选择强度段判断方式");
                    return;
                }
                obj.IntensityStageEstimateWay = FormatEstimate(IntensityStageEstimateWaySelect.Text);

                if (IntensityStageEstimateValueInput.Text.IsNullOrEmpty())
                {
                    Error("请输入强度段判断数值");
                    return;
                }
                obj.IntensityStageEstimateValue = Math.Round(double.Parse(IntensityStageEstimateValueInput.Text), 3);
                stageCount++;
            }

            if (RigorStagePressureInput.Text.Length > 0)
            {
                obj.RigorStagePressure = Math.Round(double.Parse(RigorStagePressureInput.Text), 3);

                if (RigorStageTimeInput.Text.IsNullOrEmpty())
                {
                    Error("请选择严密性持续时间");
                    return;
                }
                obj.RigorStageTime = RigorStageTimeInput.Text.ObjToInt();

                if (RigorStageTimeUnitSelect.Text.IsNullOrEmpty())
                {
                    Error("请选择严密性时间单位");
                    return;
                }
                obj.RigorStageTimeUnit = RigorStageTimeUnitSelect.Text;

                if (RigorStageEstimateWaySelect.Text.IsNullOrEmpty())
                {
                    Error("请选择严密性判断方式");
                    return;
                }
                obj.RigorStageEstimateWay = FormatEstimate(RigorStageEstimateWaySelect.Text);

                if (RigorStageEstimateValueInput.Text.IsNullOrEmpty())
                {
                    Error("请输入严密性判断数值");
                    return;
                }
                obj.RigorStageEstimateValue = Math.Round(double.Parse(RigorStageEstimateValueInput.Text), 3);
                stageCount++;
            }
            obj.StageCount = stageCount;
            if (obj.StageCount == 0)
            {
                string msg = string.Format("【{0}】模板无试压阶段，您确定要添加吗？",obj.Number);
                AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                {
                    OnOk = config =>
                    {
                        Add();
                        return true;
                    },
                });
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void Add()
        {
            List<Template> list = new List<Template>();
            list.Add(obj);
            int result = SqlHelper.Insertable(list);
            if (result > 0)
            {
                this.Close();
            }
            else
            {
                Error("添加模板失败");
            }
        }

        #endregion
    }
}
